//
//  AppDelegate.m
//  KLBookStore
//
//  Created by apple on 2017/10/10.
//  Copyright © 2017年 NetEaseExam. All rights reserved.
//

#import "AppDelegate.h"

#import "KLBookListController.h"
#import "KLBookScannerController.h"
#import "KLBookAnalysisController.h"
#import "KLNavigationController.h"

#import "KLAVManager.h"
#import "KLBookDBHelper.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    
    [self initTabbar];
    [self initDB];
    return YES;
}

- (void)initDB {
    
    NSString *dbPath = [KLBookDBHelper dbPath];
    
    BOOL dbExit = [[NSFileManager defaultManager] fileExistsAtPath:dbPath isDirectory:nil];
    
    if (!dbExit) {
        [KLBookDBHelper buildDataBase];
    }
    
#if DEBUG
    NSLog(@"dbPath: %@",dbPath);
#endif
    
}

- (void)initTabbar {
    UITabBarController *tabbarVc = [[UITabBarController alloc] init];
    tabbarVc.delegate = self;
    self.window.rootViewController = tabbarVc;
    tabbarVc.tabBar.barTintColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    tabbarVc.tabBar.tintColor = [UIColor colorWithRed:0 green:157/255.0 blue:130/255.0 alpha:1];
    
    KLBookListController *bookListVc = [[KLBookListController alloc] init];
    KLNavigationController *bookListNavVc = [[KLNavigationController alloc] initWithRootViewController:bookListVc];
    bookListVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的藏书"
                                                          image:[[UIImage imageNamed:@"tabbar-icon-collection"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                  selectedImage:[UIImage imageNamed:@"tabbar-icon-collection"]];
    
    
    KLBookScannerController *bookScannerVc = [[KLBookScannerController alloc] init];
    bookScannerVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"扫码藏书"
                                                             image:[[UIImage imageNamed:@"tabbar-icon-scan"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                     selectedImage:[UIImage imageNamed:@"tabbar-icon-scan"]];
    
    KLBookAnalysisController *bookAnalysisVc = [[KLBookAnalysisController alloc] init];
    KLNavigationController *bookAnalysisNavVc = [[KLNavigationController alloc] initWithRootViewController:bookAnalysisVc];
    bookAnalysisVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"个人中心"
                                                              image:[[UIImage imageNamed:@"tabbar-icon-me"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                      selectedImage:[UIImage imageNamed:@"tabbar-icon-me"]];
    
    tabbarVc.viewControllers = @[bookListNavVc,bookScannerVc,bookAnalysisNavVc];
    
    tabbarVc.tabBar.itemPositioning = UITabBarItemPositioningCentered;
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    if ([KLAVManager sharedInstance].captureSessionConfig) {
        [[KLAVManager sharedInstance].captureSession startRunning];
    }
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    if ([KLAVManager sharedInstance].captureSessionConfig) {
        [[KLAVManager sharedInstance].captureSession stopRunning];
    }
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    if ([KLAVManager sharedInstance].captureSessionConfig) {
        [[KLAVManager sharedInstance].captureSession startRunning];
    }
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    if ([KLAVManager sharedInstance].captureSessionConfig) {
        [[KLAVManager sharedInstance].captureSession stopRunning];
    }
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    if ([KLAVManager sharedInstance].captureSessionConfig) {
        [[KLAVManager sharedInstance].captureSession stopRunning];
    }
}

#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    if ([viewController isKindOfClass:[KLBookScannerController class]]) {
        KLBookScannerController *bookScannerVc = [[KLBookScannerController alloc] init];
        
        UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:bookScannerVc];
        
        [self.window.rootViewController presentViewController:navVc animated:YES completion:nil];
        
        return NO;
    }
    
    return YES;
    
}


@end
